package com.csust.automaticteach.demo.zhipu;

import com.csust.automaticteach.app.advisor.MyLoggerAdvisor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import static com.csust.automaticteach.constant.AppPromptConstant.MIND_MAP_SYSTEM_ROLE;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * ClassName MindMapClient
 *
 * @Description 智谱ai 客户端配置
 * @Author 钰玟
 * @Date 2025/8/11 下午10:34
 * Version 1.0
 **/
@Component
@Slf4j
public class MindMapClient {

    private ChatClient chatClient;

    public MindMapClient(ZhiPuAiChatModel chatModel) {
        this.chatClient = ChatClient
                .builder(chatModel)
                .defaultAdvisors(new MyLoggerAdvisor())
                .defaultSystem(MIND_MAP_SYSTEM_ROLE)
                .build();
    }
    public Flux<String> genMindMapByStream(String prompt, String chatId) {
        return chatClient.prompt()
                .user(prompt)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId).param(
                        CHAT_MEMORY_RETRIEVE_SIZE_KEY, 5))
                .stream()
                .content();
    }
}
